/*==========================================================================*\
 |  $Id: Initializer.java 54 2009-12-02 05:04:06Z VTWoods $
 |*-------------------------------------------------------------------------*|
 |  Copyright (C) 2009 Virginia Tech
 |
 |  This file is part of CloudSpace.
 |
 |  CloudSpace is free software; you can redistribute it and/or modify
 |  it under the terms of the GNU General Public License as published
 |  by the Free Software Foundation; either version 3 of the License, or
 |  (at your option) any later version.
 |
 |  CloudSpace is distributed in the hope that it will be useful,
 |  but WITHOUT ANY WARRANTY; without even the implied warranty of
 |  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 |  GNU General Public License for more details.
 |
 |  You should have received a copy of the GNU General Public License
 |  along with CloudSpace; if not, see <http://www.gnu.org/licenses/>.
\*==========================================================================*/

package student.web.internal;

import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.Page;
import org.zkoss.zk.ui.util.EventInterceptor;
import org.zkoss.zkplus.databind.AnnotateDataBinderInit;

import cloudspace.vm.VM;

//-------------------------------------------------------------------------
/**
 *  Initialize a ZK page for student use.
 *
 *  @author Stephen Edwards
 *  @author Last changed by $Author: VTWoods $
 *  @version $Revision: 54 $, $Date: 2009-12-02 05:04:06 +0000 (Wed, 02 Dec 2009) $
 */
public class Initializer
    extends AnnotateDataBinderInit
{
    // ----------------------------------------------------------
    /**
     * Initialize the page, which forces the bean shell Java interpreter
     * to be loaded for this page, a step that is necessary to set up
     * class loading for Java classes stored in the page's directory.
     * @param page the page to initialize
     * @param args any arguments that are provided are ignored
     */
    @Override
    public void doInit( Page page, Object[] args )
    {
    	if(VM.getThreadTag() != null)
    	{
    		VM.getThreadTag().removeDesktop(Executions.getCurrent().getDesktop().getId());
    	}
    	VM.currentVM().registerThread();
    	VM.getThreadTag().addDesktop(Executions.getCurrent().getDesktop().getId());
        super.doInit(page, args);
        
//        System.out.println( getClass().getName() + ": init( "
//            + page.getRequestPath()
//            + ")");

        // Force the Java interpreter to initialize for this page
        page.getInterpreter("java");
    }


    // ----------------------------------------------------------
    /**
     * After the data binder has done its work, install a piggyback event
     * handler to auto-update bindings.
     * @param page the page that was just composed
     * @param comps
     * @throws Exception
     */
    @Override
	public void doAfterCompose(Page page, Component[] comps) throws Exception
	{
    	if (page.getVariable("binder") != null)
    	{
    		// There is already a binder on this page, so skip initialization
    		return;
    	}
        super.doAfterCompose(page, comps);
        if (page.getDesktop().getAttribute("interceptor") == null)
        {
            Desktop desktop = page.getDesktop();
            EventInterceptor interceptor = new BindingUpdater(desktop);
            desktop.addListener(interceptor);
            desktop.setAttribute("interceptor", interceptor);
        }
//        EventListener listener = new EventListener() {
//            public void onEvent(Event event)
//            {
//                _binder.loadAll();
//            }
//        };
//        System.out.println("Adding listeners ...");
//        for(Iterator it = page.getRoots().iterator(); it.hasNext();)
//        {
//            addButtonListener((Component) it.next(), listener);
//        }
    }


    // ----------------------------------------------------------
//    private void addButtonListener(Component c, EventListener listener)
//    {
//        System.out.println("  inspecting: " + c);
//        System.out.println("    has onClick ="
//            + Events.isListened(c, Events.ON_CLICK, false));
//        if (c instanceof Button
//            || c instanceof Toolbarbutton)
//        {
//            c.addEventListener(Events.ON_CLICK, listener);
//            c.addEventListener(Events.ON_DOUBLE_CLICK, listener);
//            c.addEventListener(Events.ON_RIGHT_CLICK, listener);
//        }
////        else if (c instanceof Listbox)
////        {
////            c.addEventListener(Events.ON_SELECT, listener);
////        }
////        else if (c instanceof A)
////        {
////            c.addEventListener(Events.ON_CLICK, listener);
////        }
//        for(Iterator it = c.getChildren().iterator(); it.hasNext();)
//        {
//            addButtonListener((Component) it.next(), listener); //recursive
//        }
//    }
}
